﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAL;
using DTO;
using Utility;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Admin_DanhSachLoaiSanPham : System.Web.UI.Page
{
    protected string CurrentColumnSort
    {
        get
        {
            return ViewState["CurrentColumnSort"].ToString();
        }
        set
        {
            ViewState["CurrentColumnSort"] = value;
        }
    }
    protected int STT
    {
        get
        {
            return int.Parse(ViewState["STT"].ToString());
        }
        set
        {
            ViewState["STT"] = value;
        }
    }
    protected string OrderType
    {
        get
        {
            return ViewState["OrderType"].ToString();
        }
        set
        {
            ViewState["OrderType"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadSearch();
        }
    }

    private void LoadSearch()
    {
        try
        {
            if (int.Parse(Session["MaPhanQuyen"].ToString()) == 0 || int.Parse(Session["MaPhanQuyen"].ToString()) == 1)
            {
                btn_Delete.Visible = true;
                btn_Themnsx.Visible = true;

            }
            else
            {
                btn_Delete.Visible = false;
                btn_Themnsx.Visible = false;
            }
            STT = (pager.CurrentIndex - 1) * pager.PageSize + 1;
            int total = 0;
            Criteria cri = new Criteria();
            if (txt_tennsx.Text != " ")
                cri.add(Condition.AND, NhaSanXuatColumn.TenNhaSanXuat.ToString(), Condition.LIKE, txt_tennsx.Text);

            pager.PageSize = 3;// int.Parse(ConfigurationManager.AppSettings["pagesize"]);

            NhaSanXuatCollection nsxcollection = NhaSanXuatBUS.GET(cri.Criter, pager.CurrentIndex, pager.PageSize, NhaSanXuatColumn.TenNhaSanXuat.ToString(), OrderDirection.ASC.ToString(), out total);
            repeater_nsx.DataSource = nsxcollection;
            repeater_nsx.DataBind();
            pager.ItemCount = total;

            if (pager.ItemCount <= 0 || pager.ItemCount <= pager.PageSize)
                pager.Visible = false;
            else
                pager.Visible = true;
        }
        catch
        {
            
        }
    }
    protected void repeater_nsx_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            NhaSanXuat nsx = (NhaSanXuat)e.Item.DataItem;

            Label lb_STT = (Label)e.Item.FindControl("lb_STT");
            lb_STT.Text = Convert.ToString(STT + e.Item.ItemIndex);

            HyperLink hyp_Update = (HyperLink)e.Item.FindControl("hyp_Update");
            hyp_Update.NavigateUrl = "SuaNhaSanXuat.aspx?id=" + nsx.MaNhaSanXuat.ToString();
            HiddenField hf_ID = (HiddenField)e.Item.FindControl("hf_ID");
            hf_ID.Value = nsx.MaNhaSanXuat.ToString();
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadSearch();
    }
    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        LoadSearch();
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
            for (int i = 0; i < repeater_nsx.Items.Count; i++)
            {
                CheckBox cb_Delete = (CheckBox)repeater_nsx.Items[i].FindControl("cb_Delete");
                if (cb_Delete.Checked == true)
                {
                    HiddenField hf_ID = (HiddenField)repeater_nsx.Items[i].FindControl("hf_ID");
                    int mansx = int.Parse(hf_ID.Value);
                    // xoa

                    NhaSanXuatBUS.DELETE(mansx);
                }
            }
            LoadSearch();
    }
}